package cn.edu.ctbu.demo1.webapi;
import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Student;
import cn.edu.ctbu.demo1.service.StudentService;
import cn.edu.ctbu.demo1.vo.R;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    StudentService studentService;
    /**
     * 读取的url:/webapi/student/get/id
     * @return
     */

    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        student.setPassword("");
        return RUtil.success(student);
    }

    /**
     * 获取每个学生的个人信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/get")
    public R getStudent(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Integer id = (Integer)session.getAttribute("studentId");
        Student student = studentService.findById(id);
        System.out.println(student);
        return RUtil.success(student);
    }


    /**
     * 新增学生的方法
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public R insert(Student student){

        System.out.println("hello");
        Student student1 = studentService.insert(student);
        System.out.println(student);
        System.out.println(student1);
        return RUtil.success(student1);
    }

    /**
     * 更新学生的方法
     * @param student
     * @return
     */
    @PutMapping ("/update")
    public R update(Student student){
        //读取旧的数据
        Student oldStudent = studentService.findById(student.getId());
        if(StringUtils.isEmpty(student.getPassword())){
            //没有新密码，就是旧密码
            student.setPassword(oldStudent.getPassword());
        }
        studentService.update(student);
        return RUtil.success(student);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        studentService.delete(id);
        return RUtil.success(null);
    }
//    @GetMapping("/list1")
//    public List<Student> findByName(String name){
//
//        System.out.println(student);
//        return student;
//    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="select",defaultValue = "")Integer select,
            @RequestParam(value ="content",defaultValue = "")String content
            ){
        Page<Student> studentPage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable  pageable = PageRequest.of(page,size,Sort.by(Sort.Direction.ASC,"id"));
            studentPage = studentService.findAll(pageable);
        }
        else {
            Student student = new Student();
            Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher;
            /**
             * 如果下拉框选中的是学号
             */
            if(select.equals(1))
            {
                student.setNo(content);
                matcher = ExampleMatcher.matching().
                        withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            }
            /**
             * 如果下拉框选中的是姓名
             */
            else {
                student.setName(content);
                matcher = ExampleMatcher.matching()//模糊查询
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            }

            Example<Student> example = Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}

